/*
 * @Author: szx
 * @Date: 2022-01-04 13:30:41
 * @LastEditTime: 2022-01-04 13:44:45
 * @Description:
 * @FilePath: \leetcode\100-199\135\135.js
 */
/**
 * @param {number[]} ratings
 * @return {number}
 */
var candy = function (ratings) {
    const len = ratings.length;
    const res = new Array(len).fill(1);
    for (let i = 1; i < len; i++) {
        if (ratings[i] > ratings[i - 1]) res[i] = res[i - 1] + 1;
    }
    for (let i = len - 2; i >= 0; i--) {
        if (ratings[i] > ratings[i + 1]) res[i] = Math.max(res[i], res[i + 1] + 1);
    }
    return res.reduce((prev, curr) => prev + curr);
};
console.log(candy([1, 0, 2]));
